# encoding: utf-8
import wtforms
from wtforms import validators
from models import User

class RegistForm(wtforms.Form):
    telephone = wtforms.StringField(validators=[validators.length(min=11,max=11)])
    username = wtforms.StringField(validators=[validators.InputRequired(message=u'请输入用户名')])
    password1 = wtforms.StringField(validators=[validators.InputRequired()])
    password2 = wtforms.StringField(validators=[validators.InputRequired(),validators.EqualTo('password1',message='密码不一致')])
    
    # 自定义用户名验证器
    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已注册，请选用其它名称')

    #  # 自定义邮箱验证器
    # def validate_email(self, field):
    #     if User.query.filter_by(email=field.data).first():
    #         raise  ValidationError('该邮箱已注册使用，请选用其它邮箱')